package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.service.AccountService;
import org.petstore.service.impl.AccountServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UsernameIsExistServlet extends HttpServlet {
    private Account account;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        if (username == null || username.equals("")) {
            out.print("Empty");
            out.flush();
            out.close();
        } else {
            AccountService accountService = new AccountServiceImpl();
            account = accountService.getAccount(username);
            if (account != null) {
                out.print("Exist");
            } else {
                out.print("Not Exist");
            }
            out.flush();
            out.close();
        }
    }
}


